package org.apache.lucene.ocean.util;

import java.io.IOException;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.lucene.store.IndexOutput;

public class ByteArrayIndexOutput extends IndexOutput {
  private ByteArrayOutputStream byteArrayOutputStream;
  private int pos = 0;
  
  public ByteArrayIndexOutput() {
    byteArrayOutputStream = new ByteArrayOutputStream(1024);
  }
  
  public ByteArrayIndexOutput(ByteArrayOutputStream byteArrayOutputStream) {
    this.byteArrayOutputStream = byteArrayOutputStream;
  }
  
  public byte[] toByteArray() {
    return byteArrayOutputStream.toByteArray();
  }
  
  public long length() throws IOException {
    return byteArrayOutputStream.size();
  }
  
  public void writeByte(byte b) throws IOException {
    byteArrayOutputStream.write(b);
    pos++;
  }
  
  public void writeBytes(byte[] b, int offset, int length) throws IOException {
    byteArrayOutputStream.write(b, offset, length);
    pos += length;
  }
  
  public void flush() throws IOException {
  }

  public void close() throws IOException {
  }
  
  public long getFilePointer() {
    return (int)pos;
  }

  public void seek(long pos) throws IOException {
    throw new UnsupportedOperationException("");
  }
}
